﻿using Anke.SHManage.IDAL;
using Anke.SHManage.Model;
using Anke.SHManage.Model.ExtentionModel;
using Anke.SHManage.Model.ViewModel;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL
{

    /// <summary>
    /// 病历收费 查询
    /// </summary>
    public partial class M_PatientChargeDAL : BaseDAL<M_PatientCharge>, IM_PatientChargeDAL
    {
        IDALContext dalContext = new DALContextFactory().GetDALContext();
        AKV7ManageEntities db = DBContextFactory.GetDBContext() as AKV7ManageEntities;



        /// <summary>
        /// 获取病历收费相关
        /// </summary>
        /// <param name="taskCode"></param>
        /// <param name="PatientOrder"></param>
        public IEnumerable<PatientChargeInfo> getPatientCharge(string taskCode, string PatientOrder)
        {
            #region 2016-11-21  备份 zch
            //取药费备份
            //select 
            //Name=prd.DrugName
            //                ,Price= prd.Price  ,Counts= sum(convert(int,ceiling(prd.Dosage)))
            //                ,ChargeCounts =(case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end)
            //                --(sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1))    
            //                ,ChargeWay =prd.ChargeWay           
            //                ,FeeScale = prd.FeeScale       
            //                --,TotalPrice =prd.TotalPrice
            //                ,TotalPrice =case when (im.LimitMaxPrice <>0 )  then (case when (prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) > im.LimitMaxPrice) then im.LimitMaxPrice else    prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end) else prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end   
            //                ,ChargeType = '药费'             
            //                from M_PatientRecordDrug prd       
            //                left join I_Material im on prd.DrugCode=im.ID   
            //                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder  and im.IsActive = 1 
            //                group by prd.DrugName,prd.Price,prd.ChargeWay,prd.FeeScale,im.LimitMaxPrice 


            //取检查费备份 (措施)
            //select
            // Name=prm.RescueMeasureName 
            //,Price= isnull(prm.Price,0)
            //,Counts= sum(isnull(prm.NumberOfTimes,0))
            //,ChargeCounts = sum(isnull(prm.NumberOfTimes,0))
            //,ChargeWay =prm.ChargeWay
            //,FeeScale = 1
            // ,TotalPrice = case when im.LimitMaxPrice <>0   then (case when ( isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end ) else isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end
            //,ChargeType = '检查费'
            //from M_PatientRecordMeasure prm
            // left join   I_Material im  on prm.RescueMeasureCode = im.ID
            //where TaskCode=@TaskCode and PatientOrder =@PatientOrder and prm.OtherTypeID = 'PRMeasureType-99' 
            //and  im.IsActive=1 
            // group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice 


            //取治疗费备份 (措施)

            //select
            // Name=prm.RescueMeasureName 
            //,Price= isnull(prm.Price,0)
            //,Counts= sum(isnull(prm.NumberOfTimes,0))
            //,ChargeCounts = sum(isnull(prm.NumberOfTimes,0))
            //,ChargeWay =prm.ChargeWay
            //,FeeScale = 1
            // ,TotalPrice = case when im.LimitMaxPrice  <>0   then (case when ( isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end ) else isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end
            //,ChargeType = '治疗费'
            //from M_PatientRecordMeasure prm 
            //left join  I_Material im  on prm.RescueMeasureCode = im.ID
            //where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder and prm.OtherTypeID <> 'PRMeasureType-99' 
            //and im.IsActive=1 
            //group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice  

            #endregion

            #region 2018-05-04 zch 备份  治疗费取法更改 支持非急救
            //select
            //                      Name=prm.RescueMeasureName 
            //                      ,Price= isnull(prm.Price,0)
            //                      ,Counts= sum(isnull(prm.NumberOfTimes,0))
            //                      ,ChargeCounts = 
            //                      (case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                      when (isnull(im.FeeScale,1)<'-1')  then      
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                      then abs(im.FeeScale) 
            //                      else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                      else (
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                      then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                      else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)
            //                      ,ChargeWay =prm.ChargeWay
            //                      ,FeeScale = im.FeeScale
            //                      ,TotalPrice =
            //                      case when (im.LimitMaxPrice <>0 )  then 
            //                      (case when (prm.Price * (
            //                      case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                      when (isnull(im.FeeScale,1)<'-1')  then      
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                      then abs(im.FeeScale) 
            //                      else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                      else (
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                      then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                      else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
            //                      > im.LimitMaxPrice) then im.LimitMaxPrice
            //                      else   
            //                      prm.Price * (
            //                      case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                      when (isnull(im.FeeScale,1)<'-1')  then      
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                      then abs(im.FeeScale) 
            //                      else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                      else (
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                      then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                      else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
            //                      )end) 
            //                      else prm.Price * (
            //                      case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                      when (isnull(im.FeeScale,1)<'-1')  then      
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                      then abs(im.FeeScale) 
            //                      else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                      else (
            //                      case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                      then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                      else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
            //                      ) end 
            //                      ,ChargeType = '治疗费'
            //                      from M_PatientRecordMeasure prm 
            //                      left join  I_Material im  on prm.RescueMeasureCode = im.ID
            //                      where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and prm.OtherTypeID <> 'PRMeasureType-99' 
            //                      and im.IsActive=1 
            //                      group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale  
            #endregion

            //2016-11-29 zch  
            string SmallAidFee = Convert.ToString(ConfigurationManager.AppSettings["SmallAidFee"]);
            string MidAidFee = Convert.ToString(ConfigurationManager.AppSettings["MidAidFee"]);
            string SmallAidUnion = SmallAidFee + ".00元/次";
            string MidAidUnion = MidAidFee + ".00元/次";

            //2018-05-04 zch 治疗费取法更改
            string sqlStr = @" select 
                                     Name=prd.DrugName
		                            ,Price= prd.Price  ,Counts= sum(convert(int,ceiling(prd.Dosage)))
		                            ,ChargeCounts =(

		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end
                                    ) end)
		                            ,ChargeWay =prd.ChargeWay           
		                            ,FeeScale = prd.FeeScale       
		                            ,TotalPrice =
		                            case when (im.LimitMaxPrice <>0 )  then 
		                            (case when (prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end
                                    ) end)
		                            > im.LimitMaxPrice) then im.LimitMaxPrice
		                            else   
		                            prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end 
		                            )end) 
		                            else prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end 
		                            ) end 
		                            ,ChargeType = '药费'             
		                            from M_PatientRecordDrug prd       
		                            left join I_Material im on prd.DrugCode=im.ID   
		                            where TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and im.IsActive = 1 
		                            group by prd.DrugName,prd.Price,prd.ChargeWay,prd.FeeScale,im.LimitMaxPrice 
                             
                                union all

                                select 
                                 Name=prs.SanitationName 
                                ,Price= isnull(prs.Price,0)
                                ,Counts= sum(isnull(prs.NumberOfTimes,0))
                                ,ChargeCounts = sum(isnull(prs.NumberOfTimes,0))
                                ,ChargeWay =prs.ChargeWay
                                ,FeeScale = 1
                                ,TotalPrice = case when im.LimitMaxPrice <>0   then (case when ( isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
                                isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end ) else isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end
                                ,ChargeType = '材料费' 
                                from M_PatientRecordSanitation prs 
                                left join I_Material im  on prs.SanitationCode = im.ID 
                                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder 
                                and im.IsActive=1 
                                 group by prs.SanitationName,prs.Price,prs.ChargeWay,im.LimitMaxPrice 

							   union all

                               select
		                            Name=prm.RescueMeasureName 
		                            ,Price= isnull(prm.Price,0)
		                            ,Counts= sum(isnull(prm.NumberOfTimes,0))
		                            ,ChargeCounts = 
		                            (case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)

		                            ,ChargeWay =prm.ChargeWay
		                            ,FeeScale = im.FeeScale

		                            ,TotalPrice =
		                            case when (im.LimitMaxPrice <>0 )  then 
		                            (case when (prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
		                            > im.LimitMaxPrice) then im.LimitMaxPrice
		                            else   
		                            prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            )end) 
		                            else prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            ) end 
                                    ,ChargeType = '检查费'
		                            from M_PatientRecordMeasure prm 
		                            left join  I_Material im  on prm.RescueMeasureCode = im.ID
		                            where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and prm.OtherTypeID = 'PRMeasureType-99' 
		                            and im.IsActive=1 
		                            group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale  

                                union all

	                        select
                                    Name=prm.RescueMeasureName 
                                    ,Price=case when pr.PatientVersion='回家' and prm.RescueMeasureCode in  ('351','404') then 0 else   isnull(prm.Price,0) end 
                                    ,Counts= sum(isnull(prm.NumberOfTimes,0))
                                    ,ChargeCounts = 
                                    (case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)
                                    ,ChargeWay =prm.ChargeWay
                                    ,FeeScale = im.FeeScale
                                    ,TotalPrice = 
                                    case when pr.PatientVersion='回家' and prm.RescueMeasureCode in  ('351','404') then 0 
                                    else (
                                    case when (im.LimitMaxPrice <>0 )  then 
                                    (case when (prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
                                    > im.LimitMaxPrice) then im.LimitMaxPrice
                                    else   
                                    prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
                                    )end) 
                                    else prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
                                    ) end 
                                    ) end 
                                    ,ChargeType = '治疗费'
		                            from M_PatientRecordMeasure prm 
                                    left join M_PatientRecord pr on prm.TaskCode=pr.TaskCode and prm.PatientOrder = pr.PatientOrder
		                            left join  I_Material im  on prm.RescueMeasureCode = im.ID
		                            where  prm.TaskCode=@TaskCode and  prm.PatientOrder =@PatientOrder  and prm.OtherTypeID <> 'PRMeasureType-99' 
		                            and im.IsActive=1 
		                            group by prm.RescueMeasureName,prm.RescueMeasureCode,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale,pr.PatientVersion  

                                union all
                                select
                                Name=pr.RescueType
                                ,Price= case when RescueType='小抢救' then @SmallAidFee else @MidAidFee end
                                ,Counts=1
                                ,ChargeCounts = 1
                                ,ChargeWay = case when RescueType='小抢救' then @SmallAidUnion else  @MidAidUnion  end
                                ,FeeScale = 1
                                ,TotalPrice =case when RescueType='小抢救' then  @SmallAidFee else @MidAidFee end
                                ,ChargeType = '治疗费'
                                from  M_PatientRecord pr 
                                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder 
                                and pr.RescueType in ('中抢救','小抢救') ";

            SqlParameter[] prams = new SqlParameter[] { 
                        new SqlParameter("@TaskCode",SqlDbType.NVarChar,100),
                        new SqlParameter("@PatientOrder",SqlDbType.Int),
                        //2016-11-29 zch
                         new SqlParameter("@SmallAidFee",SqlDbType.NVarChar),
                         new SqlParameter("@MidAidFee",SqlDbType.NVarChar),
                          new SqlParameter("@SmallAidUnion",SqlDbType.NVarChar),
                           new SqlParameter("@MidAidUnion",SqlDbType.NVarChar)
                    };
            prams[0].Value = taskCode;
            prams[1].Value = PatientOrder;
            //2016-11-29 zch
            prams[2].Value = SmallAidFee;
            prams[3].Value = MidAidFee;
            prams[4].Value = SmallAidUnion;
            prams[5].Value = MidAidUnion;

            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sqlStr, prams);

            List<PatientChargeInfo> list = new List<PatientChargeInfo>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                PatientChargeInfo info = new PatientChargeInfo();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    info = new PatientChargeInfo();
                    info.Name = dr["Name"].ToString();
                    info.Price = DBConvert.ConvertDBTypeToNullableDecimal(dr["Price"]);
                    info.Counts = DBConvert.ConvertDBTypeToNullableInt(dr["Counts"]);
                    info.ChargeCounts = DBConvert.ConvertDBTypeToNullableInt(dr["ChargeCounts"]);
                    info.ChargeWay = dr["ChargeWay"].ToString();
                    string FeeScale = dr["FeeScale"].ToString() == "" ? "1" : dr["FeeScale"].ToString();
                    info.FeeScale = Convert.ToInt32(FeeScale);
                    info.TotalPrice = DBConvert.ConvertDBTypeToNullableDecimal(dr["TotalPrice"]);
                    info.ChargeType = dr["ChargeType"].ToString();

                    list.Add(info);
                }
            }

            return list;
        }


        //病历收费打印 （救护车费，等车费，公里收费，院前急救费, 全部收费总计）
        public DataTable getPatientChargePrintCar(string taskCode, int PatientOrder)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append(@"  select PatientName = mpg.PatientName,  
                                CarFee = mpg.CarFee, 
                                ChargeKM=mpg.ChargeKM,
                                WaitingFee=mpg.WaitingFee,
                                EmergencyFee=mpg.EmergencyFee, 
                                ReceivableTotal=mpg.ReceivableTotal, 
                                PaidMoney=mpg.PaidMoney
                                from M_PatientCharge mpg 
                        where TaskCode='" + taskCode + "' and PatientOrder='" + PatientOrder + "' ");

            //SqlParameter[] prams = new SqlParameter[] { 
            //            new SqlParameter("@TaskCode",SqlDbType.NVarChar,100),
            //            new SqlParameter("@PatientOrder",SqlDbType.Int)
            //        };
            //prams[0].Value = taskCode;
            //prams[1].Value = PatientOrder;

            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sqlStr.ToString(), null);
            return ds.Tables[0];
        }
        //病历收费打印 （药费，材料费，检查费，治疗费）
        public DataTable getPatientChargePrintOther(string taskCode, int PatientOrder)
        {
            #region 备份 2016-11-21 朱传海


            //select 
            //                SN='a'
            //               , Name=prd.DrugName
            //                ,ProjectCode = im.ProjectCode
            //                ,Price= prd.Price, Counts= sum(convert(int,ceiling(prd.Dosage)))
            //                ,ChargeCounts =(case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end)
            //                --(sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1))    
            //                ,ChargeWay =prd.ChargeWay           
            //                ,FeeScale = prd.FeeScale       
            //                --,TotalPrice =prd.TotalPrice
            //                ,TotalPrice =case when (im.LimitMaxPrice <>0 )  then (case when (prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) > im.LimitMaxPrice) then im.LimitMaxPrice else    prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end) else prd.Price * (case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1 else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end   
            //                ,ChargeType = ' 药费'             
            //                from M_PatientRecordDrug prd       
            //                left join I_Material im on prd.DrugCode=im.ID   
            //                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder  and im.IsActive = 1   and prd.Price>0
            //                group by prd.DrugName,prd.Price,prd.ChargeWay,prd.FeeScale,im.LimitMaxPrice,im.ProjectCode 





            //                    union all

            //                     select 
            //                    SN ='b'
            //                     ,Name=prs.SanitationName 
            //                    ,ProjectCode = im.ProjectCode
            //                    ,Price= isnull(prs.Price,0)
            //                    ,Counts= sum(isnull(prs.NumberOfTimes,0))
            //                    ,ChargeCounts = sum(isnull(prs.NumberOfTimes,0))
            //                    ,ChargeWay =prs.ChargeWay
            //                    ,FeeScale = 1
            //                    ,TotalPrice = case when im.LimitMaxPrice <>0   then (case when ( isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //                    isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end ) else isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end
            //                    ,ChargeType = '材料费' 
            //                    from M_PatientRecordSanitation prs 
            //                    left join I_Material im  on prs.SanitationCode = im.ID 
            //                    where TaskCode=@TaskCode and  PatientOrder =@PatientOrder    and isnull(prs.Price,0)>0 
            //                    and im.IsActive=1  
            //                     group by prs.SanitationName,prs.Price,prs.ChargeWay,im.LimitMaxPrice,im.ProjectCode 



            //                   union all
            //                    select
            //                    SN = 'c'
            //                     ,Name=prm.RescueMeasureName
            //                     ,ProjectCode = im.ProjectCode
            //                    ,Price= isnull(prm.Price,0)
            //                    ,Counts= sum(isnull(prm.NumberOfTimes,0))
            //                    ,ChargeCounts = sum(isnull(prm.NumberOfTimes,0))
            //                    ,ChargeWay =prm.ChargeWay
            //                    ,FeeScale = 1
            //                     ,TotalPrice = case when im.LimitMaxPrice  <>0   then (case when ( isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //                    isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end ) else isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end
            //                    ,ChargeType = '检查费'
            //                    from M_PatientRecordMeasure prm
            //                     left join   I_Material im  on prm.RescueMeasureCode = im.ID
            //                    where TaskCode=@TaskCode and PatientOrder =@PatientOrder and prm.OtherTypeID = 'PRMeasureType-99' 
            //                    and  im.IsActive=1    and isnull(prm.Price,0)>0  
            //                     group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.ProjectCode 

            //                    union all

            //                    select
            //                    SN ='d'
            //                     ,Name=prm.RescueMeasureName 
            //                    ,ProjectCode = im.ProjectCode
            //                    ,Price= isnull(prm.Price,0)
            //                    ,Counts= sum(isnull(prm.NumberOfTimes,0))
            //                    ,ChargeCounts = sum(isnull(prm.NumberOfTimes,0))
            //                    ,ChargeWay =prm.ChargeWay
            //                    ,FeeScale = 1
            //                     ,TotalPrice = case when im.LimitMaxPrice  <>0   then (case when ( isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //                    isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end ) else isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end
            //                    ,ChargeType = '治疗费'
            //                    from M_PatientRecordMeasure prm 
            //                    left join  I_Material im  on prm.RescueMeasureCode = im.ID
            //                    where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder and prm.OtherTypeID <> 'PRMeasureType-99' 
            //                    and im.IsActive=1   and isnull(prm.Price,0)>0  
            //                    group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.ProjectCode  

            //                    union all 

            //                    select 
            //                    SN= 'd'
            //                    ,Name=pr.RescueType
            //                     ,ProjectCode = ''
            //                    ,Price= case when RescueType='小抢救' then 30 else 40 end 
            //                    ,Counts=1 
            //                    ,ChargeCounts = 1 
            //                    ,ChargeWay = case when RescueType='小抢救' then '30.00元/次' else '40.00元/次' end 
            //                    ,FeeScale = 1 
            //                    ,TotalPrice =case when RescueType='小抢救' then 30 else 40 end 
            //                    ,ChargeType = '治疗费' 
            //                    from  M_PatientRecord pr 
            //                    where TaskCode=@TaskCode and  PatientOrder =@PatientOrder 
            //                    and pr.RescueType in ('中抢救','小抢救')
            #endregion

            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append(@" select 
                                    SN='a'
                                   , Name=prd.DrugName
                                    ,ProjectCode = im.ProjectCode
		                            ,Price= prd.Price  ,Counts= sum(convert(int,ceiling(prd.Dosage)))
		                            ,ChargeCounts =(
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end
                                    ) end)
		                            ,ChargeWay =prd.ChargeWay           
		                            ,FeeScale = prd.FeeScale       
		                            ,TotalPrice =
		                            case when (im.LimitMaxPrice <>0 )  then 
		                            (case when (prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end
                                    ) end)
		                            > im.LimitMaxPrice) then im.LimitMaxPrice
		                            else   
		                            prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end 
		                            )end) 
		                            else prd.Price * (
		                            case when  (isnull(prd.FeeScale,1)= '-1') then 1   
		                            when (isnull(prd.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(prd.Dosage)))/ abs(prd.FeeScale) >0 
		                            then abs(prd.FeeScale) 
		                            else  round( sum(convert(int,ceiling(prd.Dosage))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(prd.Dosage)))%isnull(prd.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(prd.Dosage)))/isnull(prd.FeeScale,1)),0) end) end 
		                            ) end 
		                            ,ChargeType = '药费'             
                                    from M_PatientRecordDrug prd       
		                            left join I_Material im on prd.DrugCode=im.ID   
		                            where TaskCode=@TaskCode and  PatientOrder =@PatientOrder  and im.IsActive = 1   and prd.Price>0
		                            group by prd.DrugName,prd.Price,prd.ChargeWay,prd.FeeScale,im.LimitMaxPrice,im.ProjectCode 
         

                                union all

                                 select 
                                SN ='b'
                                 ,Name=prs.SanitationName 
                                ,ProjectCode = im.ProjectCode
                                ,Price= isnull(prs.Price,0)
                                ,Counts= sum(isnull(prs.NumberOfTimes,0))
                                ,ChargeCounts = sum(isnull(prs.NumberOfTimes,0))
                                ,ChargeWay =prs.ChargeWay
                                ,FeeScale = 1
                                ,TotalPrice = case when im.LimitMaxPrice <>0   then (case when ( isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
                                isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end ) else isnull(prs.Price,0) *  sum(isnull(prs.NumberOfTimes,0)) end
                                ,ChargeType = '材料费' 
                                from M_PatientRecordSanitation prs 
                                left join I_Material im  on prs.SanitationCode = im.ID 
                                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder    and isnull(prs.Price,0)>0 
                                and im.IsActive=1  
                                 group by prs.SanitationName,prs.Price,prs.ChargeWay,im.LimitMaxPrice,im.ProjectCode 

                                  union all
                                   select
                                    SN = 'c'
                                    ,Name=prm.RescueMeasureName
                                    ,ProjectCode = im.ProjectCode
		                            ,Price= isnull(prm.Price,0)
		                            ,Counts= sum(isnull(prm.NumberOfTimes,0))
		                            ,ChargeCounts = 
		                            (case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)

		                            ,ChargeWay =prm.ChargeWay
		                            ,FeeScale = im.FeeScale

		                            ,TotalPrice =
		                            case when (im.LimitMaxPrice <>0 )  then 
		                            (case when (prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
		                            > im.LimitMaxPrice) then im.LimitMaxPrice
		                            else   
		                            prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            )end) 
		                            else prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            ) end 
                                    ,ChargeType = '检查费'
		                            from M_PatientRecordMeasure prm 
		                            left join  I_Material im  on prm.RescueMeasureCode = im.ID
		                            where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and prm.OtherTypeID = 'PRMeasureType-99' 
		                            and im.IsActive=1   and isnull(prm.Price,0)>0  
		                          group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale ,im.ProjectCode 
                              
                      
                                union all

                                select
                                    SN ='d'
                                    ,Name=prm.RescueMeasureName 
                                    ,ProjectCode = im.ProjectCode
		                            ,Price= isnull(prm.Price,0)
		                            ,Counts= sum(isnull(prm.NumberOfTimes,0))
		                            ,ChargeCounts = 
		                            (case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)
		                            ,ChargeWay =prm.ChargeWay
		                            ,FeeScale = im.FeeScale
		                            ,TotalPrice =
		                            case when (im.LimitMaxPrice <>0 )  then 
		                            (case when (prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
		                            > im.LimitMaxPrice) then im.LimitMaxPrice
		                            else   
		                            prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            )end) 
		                            else prm.Price * (
		                            case when  (isnull(im.FeeScale,1)= '-1') then 1   
		                            when (isnull(im.FeeScale,1)<'-1')  then      
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
		                            then abs(im.FeeScale) 
		                            else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
		                            else (
		                            case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
		                            then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
		                            else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
		                            ) end 
                                    ,ChargeType = '治疗费'
		                            from M_PatientRecordMeasure prm 
		                            left join  I_Material im  on prm.RescueMeasureCode = im.ID
		                            where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and prm.OtherTypeID <> 'PRMeasureType-99' 
		                            and im.IsActive=1  and isnull(prm.Price,0)>0  
                                    group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale,im.ProjectCode  


                                union all 

                                select 
                                SN= 'd'
                                ,Name=pr.RescueType
                                 ,ProjectCode = ''
                                ,Price= case when RescueType='小抢救' then 30 else 40 end 
                                ,Counts=1 
                                ,ChargeCounts = 1 
                                ,ChargeWay = case when RescueType='小抢救' then '30.00元/次' else '40.00元/次' end 
                                ,FeeScale = 1 
                                ,TotalPrice =case when RescueType='小抢救' then 30 else 40 end 
                                ,ChargeType = '治疗费' 
                                from  M_PatientRecord pr 
                                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder 
                                and pr.RescueType in ('中抢救','小抢救') ");
            SqlParameter[] prams = new SqlParameter[] { 
                        new SqlParameter("@TaskCode",SqlDbType.NVarChar,100),
                        new SqlParameter("@PatientOrder",SqlDbType.Int)
                    };
            prams[0].Value = taskCode;
            prams[1].Value = PatientOrder;

            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sqlStr.ToString(), prams);
            return ds.Tables[0];
        }

        //转运类型为回家，收费打印只取治疗费
        public DataTable getPatientChargePrintZhiLiao(string taskCode, int PatientOrder)
        {
            #region  2016-11-21 zch 备份
            //Name=prm.RescueMeasureName 
            //                   ,ProjectCode = im.ProjectCode
            //                   ,Price= isnull(prm.Price,0)
            //                   ,Counts= sum(isnull(prm.NumberOfTimes,0))
            //                   ,ChargeCounts = sum(isnull(prm.NumberOfTimes,0))
            //                   ,ChargeWay =prm.ChargeWay
            //                   ,FeeScale = 1
            //                    ,TotalPrice = case when im.LimitMaxPrice <>0   then (case when ( isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0))> im.LimitMaxPrice) then  im.LimitMaxPrice  else 
            //                   isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end ) else isnull(prm.Price,0) *  sum(isnull(prm.NumberOfTimes,0)) end
            //                   ,ChargeType = '治疗费'
            //                   from M_PatientRecordMeasure prm 
            //                   left join  I_Material im  on prm.RescueMeasureCode = im.ID
            //                   where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder and prm.OtherTypeID <> 'PRMeasureType-99' 
            //                   and im.IsActive=1  and isnull(prm.Price,0)>0 
            //                   group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.ProjectCode  
            #endregion

            #region 2018-05-04 zch 备份
            //select 
            //                         Name=prm.RescueMeasureName 
            //                        ,ProjectCode = im.ProjectCode
            //                        ,Price= isnull(prm.Price,0)
            //                        ,Counts= sum(isnull(prm.NumberOfTimes,0))
            //                        ,ChargeCounts = 
            //                        (case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                        when (isnull(im.FeeScale,1)<'-1')  then      
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                        then abs(im.FeeScale) 
            //                        else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                        else (
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                        then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                        else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)
            //                        ,ChargeWay =prm.ChargeWay
            //                        ,FeeScale = im.FeeScale
            //                        ,TotalPrice =
            //                        case when (im.LimitMaxPrice <>0 )  then 
            //                        (case when (prm.Price * (
            //                        case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                        when (isnull(im.FeeScale,1)<'-1')  then      
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                        then abs(im.FeeScale) 
            //                        else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                        else (
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                        then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                        else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
            //                        > im.LimitMaxPrice) then im.LimitMaxPrice
            //                        else   
            //                        prm.Price * (
            //                        case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                        when (isnull(im.FeeScale,1)<'-1')  then      
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                        then abs(im.FeeScale) 
            //                        else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                        else (
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                        then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                        else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
            //                        )end) 
            //                        else prm.Price * (
            //                        case when  (isnull(im.FeeScale,1)= '-1') then 1   
            //                        when (isnull(im.FeeScale,1)<'-1')  then      
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
            //                        then abs(im.FeeScale) 
            //                        else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
            //                        else (
            //                        case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
            //                        then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
            //                        else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
            //                        ) end 
            //                        ,ChargeType = '治疗费'
            //                        from M_PatientRecordMeasure prm 
            //                        left join  I_Material im  on prm.RescueMeasureCode = im.ID
            //                        where  TaskCode=@TaskCode and  PatientOrder =@PatientOrder   and prm.OtherTypeID <> 'PRMeasureType-99' 
            //                        and im.IsActive=1  and isnull(prm.Price,0)>0  
            //                        group by prm.RescueMeasureName,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale,im.ProjectCode  

            #endregion

            //2016-11-29 zch
            string SmallAidFee = Convert.ToString(ConfigurationManager.AppSettings["SmallAidFee"]);
            string MidAidFee = Convert.ToString(ConfigurationManager.AppSettings["MidAidFee"]);
            string SmallAidUnion = SmallAidFee + ".00元/次";
            string MidAidUnion = MidAidFee + ".00元/次";

            //2018-05-04 zch 增加非急救收费判断，如果是非急救（回家）的 吸氧（351），气压固定（404）不收费
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append(@"  
                                    select
                                    Name=prm.RescueMeasureName 
                                    ,ProjectCode = im.ProjectCode
                                    ,Price=case when pr.PatientVersion='回家' and prm.RescueMeasureCode in  ('351','404') then 0 else   isnull(prm.Price,0) end 
                                    ,Counts= sum(isnull(prm.NumberOfTimes,0))
                                    ,ChargeCounts = 
                                    (case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end)
                                    ,ChargeWay =prm.ChargeWay
                                    ,FeeScale = im.FeeScale

                                    ,TotalPrice = 
                                    case when pr.PatientVersion='回家' and prm.RescueMeasureCode in  ('351','404') then 0 
                                    else (
                                    case when (im.LimitMaxPrice <>0 )  then 
                                    (case when (prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end )
                                    > im.LimitMaxPrice) then im.LimitMaxPrice
                                    else   
                                    prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
                                    )end) 
                                    else prm.Price * (
                                    case when  (isnull(im.FeeScale,1)= '-1') then 1   
                                    when (isnull(im.FeeScale,1)<'-1')  then      
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/ abs(im.FeeScale) >0 
                                    then abs(im.FeeScale) 
                                    else  round( sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0)))),0)  end 
                                    else (
                                    case when sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))%isnull(im.FeeScale,1)>0 
                                    then round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0)+1
                                    else round((sum(convert(int,ceiling(isnull(prm.NumberOfTimes,0))))/isnull(im.FeeScale,1)),0) end) end 
                                    ) end 
                                    ) end 
                                    ,ChargeType = '治疗费'
		                            from M_PatientRecordMeasure prm 
                                    left join M_PatientRecord pr on prm.TaskCode=pr.TaskCode and prm.PatientOrder = pr.PatientOrder
		                            left join  I_Material im  on prm.RescueMeasureCode = im.ID
		                            where  prm.TaskCode=@TaskCode and  prm.PatientOrder =@PatientOrder   and prm.OtherTypeID <> 'PRMeasureType-99' 
		                            and im.IsActive=1  and isnull(prm.Price,0)>0  
                                    group by prm.RescueMeasureName,prm.RescueMeasureCode,prm.Price,prm.ChargeWay,im.LimitMaxPrice,im.FeeScale,im.ProjectCode  ,pr.PatientVersion  
 
                                union all

                                select
                                Name=pr.RescueType
                                ,ProjectCode =''
                                ,Price= case when RescueType='小抢救' then  @SmallAidFee else @MidAidFee end
                                ,Counts=1
                                ,ChargeCounts = 1
                                ,ChargeWay = case when RescueType='小抢救' then @SmallAidUnion else  @MidAidUnion  end
                                ,FeeScale = 1
                                ,TotalPrice =case when RescueType='小抢救' then @SmallAidFee else @MidAidFee end
                                ,ChargeType = '治疗费'
                                from  M_PatientRecord pr 
                                where TaskCode=@TaskCode and  PatientOrder =@PatientOrder 
                                and pr.RescueType in ('中抢救','小抢救') ");
            SqlParameter[] prams = new SqlParameter[] { 
                        new SqlParameter("@TaskCode",SqlDbType.NVarChar,100),
                        new SqlParameter("@PatientOrder",SqlDbType.Int),
                         //2016-11-29 zch
                        new SqlParameter("@SmallAidFee",SqlDbType.NVarChar),
                         new SqlParameter("@MidAidFee",SqlDbType.NVarChar),
                          new SqlParameter("@SmallAidUnion",SqlDbType.NVarChar),
                           new SqlParameter("@MidAidUnion",SqlDbType.NVarChar)

                    };
            prams[0].Value = taskCode;
            prams[1].Value = PatientOrder;
            //2016-11-29 zch
            prams[2].Value = SmallAidFee;
            prams[3].Value = MidAidFee;
            prams[4].Value = SmallAidUnion;
            prams[5].Value = MidAidUnion;

            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sqlStr.ToString(), prams);
            return ds.Tables[0];
        }

        public DataTable getPatientROSCPrint(string taskCode, int PatientOrder)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                    select  
                    AttackPlace,Witness,CarToBeforeCPR,CarToBeforeDefibrillation, 
                    ArriveSceneTime,ChestCompression,StartChestCompressionTime,BeforeResuscitationECGDiagnosis, 
                    AfterResuscitationECGDiagnosis,Defibrillation,FirstDefibrillationTime,IntervalTime, 
                    AfterResuscitationBP,AfterResuscitationSaO2,CardiacArrestReason,CardiacArrestReasonSupplement, 
                    PulsationAppearTime,BreatheAppearTime,IFAdmittedToHospital,IFLeaveHospital,LeaveHospitalDate, 
                    LeaveHospitalCPC,VerifyResults,VerifyPerson,VerifyTime, 
                    DoctorFollowUpReason,DoctorFollowUp,
                    CenterAuditPerson,CenterAuditTime,CenterAuditResult,CenterNotThroughReason 

                      from M_PatientRecordCPR 
                       where TaskCode='" + taskCode + @"' and PatientOrder=" + PatientOrder + @" 
                    ");
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
            return ds.Tables[0];
        }

        //2016-7-8 zch 新增获取重打发票的信息
        public DataTable getReChargePrint(string taskCode, int PatientOrder)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                    select 
                    isnull(InvoiceNumber,'') as 发票号,
                    isnull(TaskSD,'') as 任务流水号,
                    isnull(PatientName,'') as 姓名,
                    isnull(CarFee,0)  as  急救车费,
                    isnull(WaitingFee,0) as 等车费,
                    isnull(baidufei,0) as 摆渡费,
                    isnull(CarFee,0)  +isnull(WaitingFee,0) +isnull(baidufei,0)   as 车费合计,
                    dbo.toUppercaseRMB(isnull(CarFee,0)  +isnull(WaitingFee,0) +isnull(baidufei,0) )   as   车费大写,
                    isnull(EmergencyFee,0)    as  院前急救费,
                    isnull(DrugFeeTotal,0)  as 药费,
                    isnull(ConsumableFeeTotal,0)   as 材料费,
                    isnull( ExamineFeeTotal,0) as 检查费,
                    isnull(MeasureFeeTotal,0)  as 治疗费,
                    (isnull(EmergencyFee,0)+ isnull(DrugFeeTotal,0)+isnull(ConsumableFeeTotal,0) +isnull(ExamineFeeTotal,0) +isnull(MeasureFeeTotal,0))  as 治疗费合计,
                    dbo.toUppercaseRMB(isnull(EmergencyFee,0)+ isnull(DrugFeeTotal,0)+isnull(ConsumableFeeTotal,0) +isnull(ExamineFeeTotal,0) +isnull(MeasureFeeTotal,0))     as   治疗费大写,
                    tp.工号 as  收费工号
                    ,convert(char(10),pc.Date,120) as 日期
                    ,case when 操作状态=3 then 中心流水号 else '' end as 中心流水号
                    ,case when 操作状态=3 then 卡号 else '' end as 卡号
                    ,case when 操作状态=3 then (case when SUBSTRING(账户标志,12,1) = '0' then '城保' when SUBSTRING(账户标志,12,1) = '3' then '小时工' when SUBSTRING(账户标志,12,1) = 'A' then '民政医疗帮困'  when SUBSTRING(账户标志,12,1) = 'B' then '老年遗属'  when SUBSTRING(账户标志,12,1) = 'C' then '无保老人' when SUBSTRING(账户标志,12,1) = 'D' then '重残人员' when SUBSTRING(账户标志,12,1) = 'E' then '中小学生和婴幼儿' when SUBSTRING(账户标志,12,1) = 'F' then '其他居民' when SUBSTRING(账户标志,12,1) = 'G' then '医疗互助帮困对象' when SUBSTRING(账户标志,12,1) = 'H' then '新农合'  when SUBSTRING(账户标志,12,1) = 'Y' then '异地' end) else  '' end as 账户类型
                    ,case when 操作状态=3 then isnull(医保结算范围费用总额,0)-isnull(交易费用总额,0)+isnull(附加段现金支付数,0)+isnull(自负段现金支付数,0)+isnull(统筹段现金支付数,0) else null end as 现金支付
                    ,case when 操作状态=3 then isnull(附加段现金支付数,0)+isnull(自负段现金支付数,0)+isnull(统筹段现金支付数,0) else null  end as 自负金额
                    ,case when 操作状态=3 then isnull(非医保结算范围费用总额,0) else null end as 自费金额
                    ,case when 操作状态=3 then isnull(医保结算范围费用总额,0)-isnull(交易费用总额,0) else null end as 分类自负金额
                    ,case when 操作状态=3 then isnull(统筹支付数,0) else null end as 医保统筹支付
                    ,case when 操作状态=3 then isnull(附加支付数,0) else null end as 附加支付
                    ,case when 操作状态=3 then isnull(当年账户支付数,0)+isnull(历年账户支付数,0)+isnull(自负段历年账户支付数,0)+isnull(统筹段账户支付数,0)+isnull(附加段账户支付数,0) else null end as 个人账户支付 

                    from M_PatientCharge  pc ");
            SqlConnectionStringBuilder buildermain = new SqlConnectionStringBuilder(SqlHelper.AttemperConnectionString);
            SqlConnectionStringBuilder builderyb = new SqlConnectionStringBuilder(SqlHelper.YBConnectionString);
            sb.Append(@" left join ").Append(builderyb.InitialCatalog).Append(".dbo.M_PatientRecordYiBao mpryb on  pc.TaskCode = mpryb.任务编码 and pc.PatientOrder = mpryb.病历序号 ");
            sb.Append(@" left join ").Append(buildermain.InitialCatalog).Append(".dbo.TTaskPersonLink  tpl on pc.TaskCode = tpl.任务编码  and tpl.人员类型编码=4");//2017-07-31 将“tpl.人员类型编码=3”改为“tpl.人员类型编码=4” 周志猛
            sb.Append(@" left join ").Append(buildermain.InitialCatalog).Append(".dbo.TPerson tp on tpl.人员编码 = tp.编码  ");
            sb.Append("    where pc.TaskCode='" + taskCode + @"' and  pc.PatientOrder=" + PatientOrder + "   order by 交易时间 desc ");
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
            return ds.Tables[0];
        }


        //2018-05-04 zch
        public List<M_PatientChargeDicInfo> GetChargeDictionary()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select   ID
                                  ,TypeCode
                                  ,Formula
                                  ,PriceOfKM
                                  ,OriginKM
                                  ,LeastPrice
                                  ,isValid
                                  ,FirstAidPrice
                              FROM M_PatientChargeDictionary 
                               where isValid = 1 ");
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);

            List<M_PatientChargeDicInfo> list = new List<M_PatientChargeDicInfo>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    M_PatientChargeDicInfo info = new M_PatientChargeDicInfo();
                    info.TypeCode = Convert.ToInt32(dr["TypeCode"]);
                    info.Formula = dr["Formula"].ToString();
                    info.PriceOfKM = Convert.ToDouble(dr["PriceOfKM"]);
                    info.OriginKM = Convert.ToInt32(dr["OriginKM"]);
                    info.LeastPrice = Convert.ToDouble(dr["LeastPrice"]);
                    info.FirstAidPrice = Convert.ToDouble(dr["FirstAidPrice"]);
                    list.Add(info);
                }
            }
            return list;

        }

        //2018-05-04 zch
        public M_PatientChargeDicInfo GetChargeDictionary(string TypeCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select     ID
                                  ,TypeCode
                                  ,Formula
                                  ,PriceOfKM
                                  ,OriginKM
                                  ,LeastPrice
                                  ,isValid
                                  ,FirstAidPrice
                              FROM M_PatientChargeDictionary 
                               where isValid = 1 ");
            WhereClauseUtility.AddStringEqual("TypeCode", TypeCode, sb);
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                M_PatientChargeDicInfo info = new M_PatientChargeDicInfo();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    info.TypeCode = Convert.ToInt32(dr["TypeCode"]);
                    info.Formula = dr["Formula"].ToString();
                    info.PriceOfKM = Convert.ToDouble(dr["PriceOfKM"]);
                    info.OriginKM = Convert.ToInt32(dr["OriginKM"]);
                    info.LeastPrice = Convert.ToDouble(dr["LeastPrice"]);
                    info.FirstAidPrice = Convert.ToDouble(dr["FirstAidPrice"]);
                }
                return info;
            }
            else { return null; }
        }

        /// <summary>
        /// add2021-09-09 sr
        /// add2021-10-19 plq 医保上线移植
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool UpdateChargeLog(M_PatientCharge info)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"update [M_PatientChargeLog] set Date='" + info.Date + "',InvoiceNumber =  '" + info.InvoiceNumber + "',PatientName='" + info.PatientName + "',AddressStart='" + info.AddressStart + "',AddressEnd='" + info.AddressEnd + "',OutStationRoadCode='" + info.OutStationRoadCode + "',PointRoadCode='" + info.PointRoadCode + "',ArriveHospitalRoadCode='" + info.ArriveHospitalRoadCode + "',ReturnStationRoadCode='" + info.ReturnStationRoadCode + "',OneWayKM='" + info.OneWayKM + "',ChargeKM='" + info.ChargeKM + "',CarFee='" + info.CarFee + "',WaitingFee='" + info.WaitingFee + "',EmergencyFee='" + info.EmergencyFee + "',DrugFeeTotal='" + info.DrugFeeTotal + "',ExamineFeeTotal='" + info.ExamineFeeTotal + "',ConsumableFeeTotal='" + info.ConsumableFeeTotal + "',MeasureFeeTotal='" + info.MeasureFeeTotal + "',ReceivableTotal='" + info.ReceivableTotal + "',PaidMoney='" + info.PaidMoney + "',ChargePerson='" + info.ChargePerson + "',IFArrearage='" + info.IFArrearage + "',ReceivablesPaidInconsistentReasons='" + info.ReceivablesPaidInconsistentReasons + "',BackUp1='" + info.BackUp1 + "',baidufei='" + info.baidufei + "',PaidCarFee='" + info.PaidCarFee + "',PaidPRFee='" + info.PaidPRFee + "'  where TaskCode='" + info.TaskCode + "' and PatientOrder = '" + info.PatientOrder + "' ");
            int ds = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
            return ds > 0 ? true : false;
        }

    }

}
